package com.atguigu.auth.controller.api;

import com.atguigu.auth.service.OaProcessService;
import com.atguigu.auth.service.OaProcessTemplateService;
import com.atguigu.auth.service.OaProcessTypeService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.Result;
import com.atguigu.model.process.Process;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.vo.process.ApprovalVo;
import com.atguigu.vo.process.ProcessFormVo;
import com.atguigu.vo.process.ProcessVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ProcessController
 * Package: com.atguigu.auth.controller.api
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/22 8:50
 * @Version 1.0
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping(value = "/admin/process")
@CrossOrigin  // 跨域
public class ProcessApiController {
  @Autowired
  private OaProcessTypeService processTypeService;
  
  @Autowired
  private OaProcessTemplateService processTemplateService;
  
  @Autowired
  private OaProcessService processService;
  
  @Autowired
  private SysUserService userService;
  
  @ApiOperation(value = "获取全部审批分类及每个分类下的模板")
  @GetMapping("findProcessType")
  public Result findProcessType() {
    return Result.ok(processTypeService.findProcessType());
  }
  
  
  @ApiOperation(value = "获取审批模板")
  @GetMapping("getProcessTemplate/{processTemplateId}")
  public Result get(@PathVariable Long processTemplateId) {
    ProcessTemplate processTemplate = processTemplateService.getById(processTemplateId);
    return Result.ok(processTemplate);
  }
  
  @ApiOperation(value = "启动流程")
  @PostMapping("/startUp")
  public Result start(@RequestBody ProcessFormVo processFormVo) {
    processService.startUp(processFormVo);
    return Result.ok();
  }
  
  @ApiOperation(value = "待处理")
  @GetMapping("/findPending/{page}/{limit}")
  public Result findPending(
      @ApiParam(name = "page", value = "当前页码", required = true)
      @PathVariable Long page,
      
      @ApiParam(name = "limit", value = "每页记录数", required = true)
      @PathVariable Long limit) {
    Page<java.lang.Process> pageParam = new Page<>(page, limit);
    IPage<ProcessVo> pending = processService.findPending(pageParam);
    return Result.ok(pending);
  }
  
  @ApiOperation(value = "获取审批详情")
  @GetMapping("show/{id}")
  public Result show(@PathVariable Long id) {
    return Result.ok(processService.show(id));
  }
  
  @ApiOperation(value = "审批")
  @PostMapping("approve")
  public Result approve(@RequestBody ApprovalVo approvalVo) {
    processService.approve(approvalVo);
    return Result.ok();
  }
  
  @ApiOperation(value = "已处理")
  @GetMapping("/findProcessed/{page}/{limit}")
  public Result findProcessed(
      @ApiParam(name = "page", value = "当前页码", required = true)
      @PathVariable Long page,
      @ApiParam(name = "limit", value = "每页记录数", required = true)
      @PathVariable Long limit) {
    Page<Process> pageParam = new Page<>(page, limit);
    IPage<ProcessVo> processed = processService.findProcessed(pageParam);
    return Result.ok(processed);
  }
  
  @ApiOperation(value = "已发起")
  @GetMapping("/findStarted/{page}/{limit}")
  public Result findStarted(
      @ApiParam(name = "page", value = "当前页码", required = true)
      @PathVariable Long page,
      
      @ApiParam(name = "limit", value = "每页记录数", required = true)
      @PathVariable Long limit) {
    Page<ProcessVo> pageParam = new Page<>(page, limit);
    return Result.ok(processService.findStarted(pageParam));
  }
  
  @ApiOperation(value = "获取当前用户基本信息")
  @GetMapping("getCurrentUser")
  public Result getCurrentUser() {
    return Result.ok(userService.getCurrentUser());
  }
}
